package com.hqq.concurrency.read_write_lock;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.util.concurrent.locks.Lock;

/**
 * Writer
 * Created by heqianqian on 2017/7/30.
 */
public class Writer extends Thread {

    private static final Logger LOGGER = LoggerFactory.getLogger(Writer.class);

    private String name;

    private Lock writeLock;

    public Writer(String name, Lock writeLock) {
        this.name = name;
        this.writeLock = writeLock;
    }

    @Override
    public void run() {
        writeLock.lock();
        try {
            write();
        } catch (InterruptedException e) {
            e.printStackTrace();
        } finally {
            writeLock.unlock();
        }
    }

    public void write() throws InterruptedException {
        LOGGER.info("{} write begin", name);
        Thread.sleep(500);
        LOGGER.info("{} write end", name);
    }
}
